class Solution {
public:
    string reverseWords(string s) {
        int idx = 0;
        while (idx < s.size()) {
            int l = idx;
            int r = idx;
            while (s[r] != ' ' && r < s.size())r++;
            //cout<<"r=="<<r<<endl;
            idx = r + 1;
            r--;
            while (l <= r) {
                swap(s[l], s[r]);
                r--;
                l++;
            }
        }
        return s;
    }
};